<template>
  <Card title="7天收运完成率" :loading="loading">
    <!-- <VisitAnalysis /> -->
    <div ref="chartRef" :style="{ height, width }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  // import VisitAnalysis from './VisitAnalysis.vue';
  import { useECharts } from '/@/hooks/web/useECharts';

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '280px',
    },
    res:{
      type: Array,
      default: {
        data:{}
      },
    }
  });

  async function handleData() {
    // const res = await getPreQtyCensus();
    let ddateArr = []
    let overRateArr = []
    props.res.data.forEach(item=>{
      ddateArr.push(item.ddate)
      overRateArr.push(item.overRate)
    })
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      xAxis: {
        type: 'category',
        // boundaryGap: false,
        data: ddateArr,
        splitLine: {
          show: true,
          lineStyle: {
            width: 1,
            type: 'solid',
            color: 'rgba(226,226,226,0.5)',
          },
        },
        axisTick: {
          show: false,
        },
      },
      yAxis: [
        {
          type: 'value',
          max: 100,
          // splitNumber: 4,
          axisTick: {
            show: false,
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(255,255,255,0.2)'],
              // color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
            },
          },
        },
      ],
      grid: { left: '1%', right: '1%', top: '2  %', bottom: 0, containLabel: true },
      series: [
        {
          name: '完成率',
          smooth: true,
          data: overRateArr,
          type: 'line',
          areaStyle: {},
          itemStyle: {
            color: '#5ab1ef',
          },
        },
        // {
        //   smooth: true,
        //   data: [
        //     33, 66, 88, 333, 3333, 5000, 18000, 3000, 1200, 13000, 22000, 11000, 2221, 1201, 390,
        //     198, 60, 30, 22, 11,
        //   ],
        //   type: 'line',
        //   areaStyle: {},
        //   itemStyle: {
        //     color: '#019680',
        //   },
        // },
      ],
    });
  }

  watch(
    () => [props.loading,props.res],
    () => {
      if (props.loading) {
        return;
      }
      handleData()
    },
    { immediate: true },
  );
</script>
